
#include <fstream>
#include <iostream>
#include <vector>

// #include "util.hpp"
// #include "level.hpp"
// #include "message.hpp"
// #include "format.hpp"
// #include "sink.hpp"
// #include "logger.hpp"
// #include "buffer.hpp"

#include "../Logs/nhylog.hpp"


void test_log()
{
    nhylog::Logger::ptr logger = nhylog::LoggerManager::getInstance().getLogger("async_logger");

    // logger->debug(__FILE__,__LINE__,"%s","测试输出");
    // logger->info(__FILE__,__LINE__,"%s","测试输出");
    // logger->warn(__FILE__,__LINE__,"%s","测试输出");
    // logger->error(__FILE__,__LINE__,"%s","测试输出");
    // logger->fatal(__FILE__,__LINE__,"%s","测试输出");


    // //宏替换后，自动加上行号
    // logger->debug("%s","测试输出");
    // logger->info("%s","测试输出");
    // logger->warn("%s","测试输出");
    // logger->error("%s","测试输出");
    // logger->fatal("%s","测试输出");

    //宏替换后，自动加上行号
    DEBUG("%s","测试输出");
    INFO("%s","测试输出");
    WARN("%s","测试输出");
    ERROR("%s","测试输出");
    FATAL("%s","测试输出");

    int count = 0;
    while(count < 500000)
    {
        logger->fatal("测试日志-%d",count++);
    }

}
int main()
{   

    // //读取文件数据，一点一点写入缓冲区，再将缓冲区的数据写入文件，判断新文件和源文件是否一致
    // std::ifstream ifs("./msg.log",std::ios::binary);
    // if(!ifs.is_open())
    // {
    //     std::cout << "open error" << std::endl;
    //     return -1;
    // } 
    // ifs.seekg(0,std::ios::end);//跳转到文件末尾
    // size_t fsize = ifs.tellg();//获取当前位置相对于末尾的长度
    // ifs.seekg(0,std::ios::beg);//重新跳转回起始位置

    // std::cout<< "fsize:" << fsize << std::endl;


    // std::string body;
    // body.resize(fsize);
    // //不能用cstr，因为cstr是const的
    // ifs.read(&body[0],fsize);

    // std::cout << body << std::endl;

    // if(!ifs.good())
    // {
    //     std::cout << "read error" << std::endl;
    //     return -1;
    // }

    // ifs.close();

    // nhylog::Buffer buffer;

    // //放到buffer里
    // //buffer.push(body.c_str(),body.size());
    // for(int i = 0;i<body.size();i++)
    // {
    //     buffer.push(&body[i],1);
    // }
    
    // std::ofstream ofs("./new.log",std::ios::binary);
    // //ofs.write(buffer.begin(),buffer.readAbleSize());
    
    // //i在++，同时buffer的readAbleSize也在变化
    // size_t rsize = buffer.readAbleSize();
    // for(int i = 0;i<rsize;i++)
    // {
    //     ofs.write(buffer.begin(),1);
    //     buffer.moveReader(1);
    // }
    
    //md5sum 来测试两个文件是否一致

    std::unique_ptr<nhylog::LoggerBuider> builder(new nhylog::GlobleLoggerBuider());
    //指挥者的目的是让对象的各个零件按照一定顺序去构造，这里不需要顺序，因此不需要指挥者类
    builder->buildLoggerName("async_logger");
    builder->buildLoggerType(nhylog::LoggerType::LOGGER_ASYNC);
    builder->buildEnableUnSafeAsync();
    builder->buildLoggerLevel(nhylog::LogLevel::value::WARN);
    builder->buildLoggerFormatter("[%c][%f:%l]%m%n");
    builder->buildSink<nhylog::FileSink>("./msg.log");
    builder->buildSink<nhylog::StdoutSink>();
    builder->build();

    // nhylog::Logger::ptr logger = builder->build();

    // logger->debug(__FILE__,__LINE__,"%s","测试输出");
    // logger->info(__FILE__,__LINE__,"%s","测试输出");
    // logger->warn(__FILE__,__LINE__,"%s","测试输出");
    // logger->error(__FILE__,__LINE__,"%s","测试输出");
    // logger->fatal(__FILE__,__LINE__,"%s","测试输出");

    // int count = 0;
    // while(count < 500000)
    // {
    //     logger->fatal(__FILE__,__LINE__,"测试日志-%d",count++);
    // }

    
    test_log();

    // std::string logger_name = "sync_logger";//日志器名称
    // nhylog::LogLevel::value limit = nhylog::LogLevel::value::WARN;//限制等级
    // nhylog::Formatter::ptr fmt(new nhylog::Formatter());
    
    //三个落地方向
    // nhylog::LogSink::ptr Stdout_lsp = nhylog::SinkFactory::create<nhylog::StdoutSink>();
    // nhylog::LogSink::ptr file_lsp = nhylog::SinkFactory::create<nhylog::FileSink>("./logAsync/file.log");
    // nhylog::LogSink::ptr Roll_lsp = nhylog::SinkFactory::create<nhylog::RollBySizeSink>("./logAsync/RollBySize", 1024 * 1024);

    // std::vector<nhylog::LogSink::ptr> sinks = {Stdout_lsp,file_lsp,Roll_lsp};

    // nhylog::Logger::ptr logger(new nhylog::SyncLogger(logger_name,limit,fmt,sinks));

    // logger->debug(__FILE__,__LINE__,"%s","测试输出");
    // logger->info(__FILE__,__LINE__,"%s","测试输出");
    // logger->warn(__FILE__,__LINE__,"%s","测试输出");
    // logger->error(__FILE__,__LINE__,"%s","测试输出");
    // logger->fatal(__FILE__,__LINE__,"%s","测试输出");

    // size_t cursize = 0,count = 0;
    // while(cursize < 1024 * 1024 * 10)
    // {
    //     logger->fatal(__FILE__,__LINE__,"测试日志-%d",count++);
    //     cursize += 20;
    // }
    
    //-----------------------------------------------------------------------------------------

    // 格式化功能测试
    // nhylog::LogMsg msg(nhylog::LogLevel::value::INFO, 12, "test.cc", "root", "格式化功能测试");
    // nhylog::Formatter fmt("[%p][%f:%l][%d{%H:%M:%S}][%c]thread-id:[%t]info:%T%m%n");
    // // nhylog::Formatter fmt("[%p][%f:%l][%d{%H:%M:%S][%c]thread-id:[%t]info:%T%m%n");

    // std::string str = fmt.format(msg);
    // std::cout << str << std::endl;
    // 23:44:43 thread::id of a non-executing thread root test.cc 12 INFO   格式化功能测试

    //-----------------------------------------------------------------------------------------

    // nhylog::LogSink::ptr lsp = nhylog::SinkFactory::create<nhylog::StdoutSink>();
    // nhylog::LogSink::ptr file_lsp = nhylog::SinkFactory::create<nhylog::FileSink>("./logfile/file.log");
    // nhylog::LogSink::ptr Roll_lsp = nhylog::SinkFactory::create<nhylog::RollBySizeSink>("./logfile/RollBySize", 1024 * 1024);
    // nhylog::LogSink::ptr Rt_ptr = nhylog::SinkFactory::create<RollByTimeSink>("./logfile/RollByTime/", GAP::GAP_SECOND);

    // lsp->log(str.c_str(), str.size());
    // file_lsp->log(str.c_str(), str.size());

    // int cursize = 0;
    // int count = 0;
    // while (cursize < 1024 * 1024 * 10)
    // {
    //     std::string temp = str + std::to_string(count++);
    //     Roll_lsp->log(temp.c_str(), temp.size());

    //     cursize += temp.size();
    // }

    //-----------------------------------------------------------------------------------------
    // util测试
    // std::cout << nhylog::util::Date::now()<< std::endl;
    // std::string pathname = "./abc/bcd/a.html";

    // nhylog::util::File::createDirectory(nhylog::util::File::path(pathname));

    return 0;
}